
import { createStore } from 'vuex'
function storeLocal(key, json) {
  let value = JSON.stringify(json);
  localStorage.setItem(key, value)
};
function getLocal(key) {
  let local_json = localStorage.getItem(key);
  if (local_json != null && local_json!="undefined") {
    return JSON.parse(local_json);
  } else {
    return null;
  }
};

function removeLocal(key) {
  localStorage.removeItem(key);
};

function storeSession(key, json) {
  let value = JSON.stringify(json);
  sessionStorage.setItem(key, value)
};
function getSession(key) {
  let local_json = sessionStorage.getItem(key);
  if (local_json != null && local_json !="undefined") {
    return JSON.parse(local_json);
  } else {
    return null;
  }
};

export default createStore({
  state: {
    // user_info:{},
    export_current: [],
    search_history_list:[],
    processFileLoading: false,
    main_node_num: 10
  },
  getters: {
    getuser_info: () => getLocal("user_info"),
    getsearched_list: () => {
      let list=getSession("search_history");
      if(list==null){
        list=[[],""]
      }
      return list;
    },
    getexport_list: state =>{
      let temp_list= getLocal("export_list");
      if(temp_list==null || temp_list== undefined){
        return [[],state.export_current]
      }else{
        return [temp_list,state.export_current]
      }
    },
    getPrecessFileLoading: state => state.processFileLoading,
    getmain_node_num: () => getSession("main_node_num")
  },
  mutations: {
    setuser_info(state, new_user_info) {
      storeLocal("user_info", new_user_info);
    },
    setsearched_list(state, list) {
      if(list[1]==""||list[1]==undefined){
        return;
      }
      storeSession("search_history",list);
    },
    setexport_list(state, list) {
      storeLocal("export_list", list[0]);
      state.export_current=list[1]
    },
    removeExportList(){
      removeLocal("export_list")
    },
    setProcessFileLoading(state, loading) {
      state.processFileLoading = loading
    },
    setmain_node_num(state, node_num) {
      console.log("set mian node num ", node_num)
      storeSession("main_node_num", node_num)
    }
  },
  actions: {
  },
  modules: {
  }
})
